/**
 * Project: greenline-prepose-client-biz-basic
 * 
 * File Created at 2013-12-2
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package org.xukai.common.util.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
 * @Type ServiceBeanFactory
 * @Desc
 * @author jianyun.zheng
 * @date 2013-12-2
 * @Version V1.0
 */
public class ServiceBeanFactory implements ApplicationContextAware {
    // private final Log logger = LogFactory.getLog(getClass());
    private ApplicationContext applicationContext;

    private String dynamicPath;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @SuppressWarnings("unchecked")
    public <T> T getBean(String bean, Class<T> clazz) {
        return (T) applicationContext.getBean(bean);
    }

    public Object getBean(String bean) {
        return applicationContext.getBean(bean);
    }

    public <T> Object getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public ConfigurableApplicationContext getConfigurableContext() {
        return (ConfigurableApplicationContext) applicationContext;
    }

    /**
     * 向spring的beanFactory动态地装载bean<br/>
     * 要装载的bean所在的xml配置文件位置<br/>
     * spring配置中的contextConfigLocation
     * 同样支持诸如"/WEB-INF/ApplicationContext-*.xml"的写法。
     * 
     * @param configLocations
     * @return
     */

    public boolean loadBean(String[] configLocations) {
        try {
            XmlWebApplicationContext ac = (XmlWebApplicationContext) this.getApplicationContext();
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
            XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
                    (BeanDefinitionRegistry) ac.getBeanFactory());
            beanDefinitionReader.setResourceLoader(ac);
            beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(ac));
            for (int i = 0; i < configLocations.length; i++) {
                beanDefinitionReader.loadBeanDefinitions(ac.getResources(configLocations[i]));
            }
//             ac.refresh();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public String getDynamicPath() {
        return dynamicPath;
    }

    public void setDynamicPath(String dynamicPath)  {
        if (null == dynamicPath || "".equals(dynamicPath)) {
            throw new RuntimeException("dynamicPath注入值未设置");
        }
        if (!dynamicPath.endsWith("/")) {
            dynamicPath += "/";
        }
        this.dynamicPath = dynamicPath;
    }
}
